use serde::{Deserialize, Serialize};

/// 用户状态
#[derive(Debug, Default, Deserialize, Serialize, sqlx::Type, Clone, Copy)]
#[sqlx(type_name = "user_status")]
pub enum Status {
    /// 待激活
    #[default]
    Pending = 0,
    /// 正常，已激活
    Actived = 1,
    /// 被冻结
    Freezed = 2,
}

/// 用户角色
#[derive(Debug, Default, Deserialize, Serialize, sqlx::Type, Clone, Copy)]
#[sqlx(type_name = "user_role")]
pub enum Role {
    /// 普通用户
    #[default]
    Member = 0,
    /// 管理员
    Admin = 1,
}

impl Role {
    pub fn is_admin(&self) -> bool {
        if let &Self::Admin = self {
            true
        } else {
            false
        }
    }
    pub fn is_member(&self) -> bool {
        if let &Self::Member = self {
            true
        } else {
            false
        }
    }
}

/// 用户
#[derive(Debug, Default, Deserialize, Serialize, Clone, sqlx::FromRow)]
pub struct User {
    pub id: String,
    /// 邮箱
    pub email: String,
    /// 密码
    #[serde(skip_serializing)]
    pub password: String,
    /// 状态
    pub status: Status,
    /// 角色
    pub role: Role,
    pub dateline: chrono::DateTime<chrono::Local>,
}
